<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- This file documents the use of the GNU compilers.

Copyright (C) 1988-2023 Free Software Foundation, Inc.

Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "Funding Free Software", the Front-Cover
Texts being (a) (see below), and with the Back-Cover Texts being (b)
(see below).  A copy of the license is included in the section entitled
"GNU Free Documentation License".

(a) The FSF's Front-Cover Text is:

A GNU Manual

(b) The FSF's Back-Cover Text is:

You have freedom to copy and modify this GNU Manual, like GNU
     software.  Copies published by the Free Software Foundation raise
     funds for GNU development. -->
<!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Variable Length (Using the GNU Compiler Collection (GCC))</title>

<meta name="description" content="Variable Length (Using the GNU Compiler Collection (GCC))">
<meta name="keywords" content="Variable Length (Using the GNU Compiler Collection (GCC))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<link href="index.html#Top" rel="start" title="Top">
<link href="Indices.html#Indices" rel="index" title="Indices">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="C-Extensions.html#C-Extensions" rel="up" title="C Extensions">
<link href="Variadic-Macros.html#Variadic-Macros" rel="next" title="Variadic Macros">
<link href="Empty-Structures.html#Empty-Structures" rel="prev" title="Empty Structures">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smalllisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>


</head>

<body lang="en_US">
<a name="Variable-Length"></a>
<div class="header">
<p>
Next: <a href="Variadic-Macros.html#Variadic-Macros" accesskey="n" rel="next">Variadic Macros</a>, Previous: <a href="Empty-Structures.html#Empty-Structures" accesskey="p" rel="prev">Empty Structures</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Indices.html#Indices" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Arrays-of-Variable-Length"></a>
<h3 class="section">6.20 Arrays of Variable Length</h3>
<a name="index-variable_002dlength-arrays"></a>
<a name="index-arrays-of-variable-length"></a>
<a name="index-VLAs"></a>

<p>Variable-length automatic arrays are allowed in ISO C99, and as an
extension GCC accepts them in C90 mode and in C++.  These arrays are
declared like any other automatic arrays, but with a length that is not
a constant expression.  The storage is allocated at the point of
declaration and deallocated when the block scope containing the declaration
exits.  For
example:
</p>
<div class="smallexample">
<pre class="smallexample">FILE *
concat_fopen (char *s1, char *s2, char *mode)
{
  char str[strlen (s1) + strlen (s2) + 1];
  strcpy (str, s1);
  strcat (str, s2);
  return fopen (str, mode);
}
</pre></div>

<a name="index-scope-of-a-variable-length-array"></a>
<a name="index-variable_002dlength-array-scope"></a>
<a name="index-deallocating-variable-length-arrays"></a>
<p>Jumping or breaking out of the scope of the array name deallocates the
storage.  Jumping into the scope is not allowed; you get an error
message for it.
</p>
<a name="index-variable_002dlength-array-in-a-structure"></a>
<p>As an extension, GCC accepts variable-length arrays as a member of
a structure or a union.  For example:
</p>
<div class="smallexample">
<pre class="smallexample">void
foo (int n)
{
  struct S { int x[n]; };
}
</pre></div>

<a name="index-alloca-vs-variable_002dlength-arrays"></a>
<p>You can use the function <code>alloca</code> to get an effect much like
variable-length arrays.  The function <code>alloca</code> is available in
many other C implementations (but not in all).  On the other hand,
variable-length arrays are more elegant.
</p>
<p>There are other differences between these two methods.  Space allocated
with <code>alloca</code> exists until the containing <em>function</em> returns.
The space for a variable-length array is deallocated as soon as the array
name&rsquo;s scope ends, unless you also use <code>alloca</code> in this scope.
</p>
<p>You can also use variable-length arrays as arguments to functions:
</p>
<div class="smallexample">
<pre class="smallexample">struct entry
tester (int len, char data[len][len])
{
  /* <span class="roman">&hellip;</span> */
}
</pre></div>

<p>The length of an array is computed once when the storage is allocated
and is remembered for the scope of the array in case you access it with
<code>sizeof</code>.
</p>
<p>If you want to pass the array first and the length afterward, you can
use a forward declaration in the parameter list&mdash;another GNU extension.
</p>
<div class="smallexample">
<pre class="smallexample">struct entry
tester (int len; char data[len][len], int len)
{
  /* <span class="roman">&hellip;</span> */
}
</pre></div>

<a name="index-parameter-forward-declaration"></a>
<p>The &lsquo;<samp>int len</samp>&rsquo; before the semicolon is a <em>parameter forward
declaration</em>, and it serves the purpose of making the name <code>len</code>
known when the declaration of <code>data</code> is parsed.
</p>
<p>You can write any number of such parameter forward declarations in the
parameter list.  They can be separated by commas or semicolons, but the
last one must end with a semicolon, which is followed by the &ldquo;real&rdquo;
parameter declarations.  Each forward declaration must match a &ldquo;real&rdquo;
declaration in parameter name and data type.  ISO C99 does not support
parameter forward declarations.
</p>
<hr>
<div class="header">
<p>
Next: <a href="Variadic-Macros.html#Variadic-Macros" accesskey="n" rel="next">Variadic Macros</a>, Previous: <a href="Empty-Structures.html#Empty-Structures" accesskey="p" rel="prev">Empty Structures</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Indices.html#Indices" title="Index" rel="index">Index</a>]</p>
</div>



</body>
</html>
